package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.StoreHostDao;
import com.bocloud.cmp.entity.resource.StoreHost;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;

/**
 * 角色-权限DAO接口实现
 * 
 * @author weiwei
 * @version 1.0
 * @since 2016.12.29
 *
 */
@Repository("storeHostDao")
public class StoreHostDaoImpl extends BasicDao implements StoreHostDao {

	@Override
	public StoreHost save(StoreHost storeHost) throws Exception {
		return (StoreHost) this.saveEntity(storeHost);
	}

	@Override
	public boolean update(StoreHost storeHost) throws Exception {
		return this.updateEntity(storeHost);
	}

	@Override
	public boolean delete(StoreHost storeHost) throws Exception {
		return this.baseDelete(storeHost);
	}

	@Override
	public List<StoreHost> list(Long vhostId, String category) throws Exception {
		String sql = "select * from store_host where vhost_id = :vhostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("vhostId", vhostId);
		params.put("category", category);
		List<Object> list = this.queryForList(sql, params, StoreHost.class);
		List<StoreHost> result = new ArrayList<>();
		for (Object object : list) {
			result.add((StoreHost) object);
		}
		return result;
	}

	@Override
	public boolean deleteByHost(Long vhostId, String category) throws Exception {
		String sql = "delete from store_host where vhost_id = :vhostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("vhostId", vhostId);
		params.put("category", category);
		return this.update(sql, params) > 0;
	}

	@Override
	public boolean deleteByStore(Long storeId) throws Exception {
		String sql = "delete from store_host where store_id = :storeId";
		Map<String, Object> params = MapTools.simpleMap("storeId", storeId);
		return this.update(sql, params) > 0;
	}

	@Override
	public boolean removeByVendor(Long vendorId) throws Exception {
		String sql = "delete from store_host where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.update(sql, params) > 0;
	}
}
